﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Konstaller
{
    public class ListBox
    {

        public List<string> Item = new List<string>();

        //---  PRIVATE FIELD ----//
        int w = 20;
        int h = 20;
        int _x = 0;
        int _y = 0;
        bool _sel;
        int _index;

        ConsoleColor bg = ConsoleColor.Blue;
        ConsoleColor fnt = ConsoleColor.White;

        const char _topleft = '\u2554';
        const char _hline = '\u2550';
        const char _topright = '\u2557';
        const char _vline = '\u2551';
        const char _bleft = '\u255A';
        const char _bright = '\u255D';

        /// <summary>
        /// Get or Set if the Control are Selected
        /// </summary>
        public bool Selected
        {
            get { return _sel; }
            set { _sel = value; }
        }



        /// <summary>
        /// Get or set Selected Item index
        /// </summary>
        public int Selected_Index
        {
            get { return _index; }
            set { _index = value; }
        }


        /// <summary>
        /// Get or Set Background Color
        /// </summary>
        public ConsoleColor BackgroundColor
        {
            get { return bg; }
            set { bg = value; }
        }


        /// <summary>
        /// Get or Set Font Color
        /// </summary>
        public ConsoleColor Foreground
        {
            get { return fnt; }
            set { fnt = value; }
        }


        /// <summary>
        /// Set X Poisition
        /// </summary>
        public int X
        {
            get { return _x; }
            set { _x = value; }
        }


        /// <summary>
        /// Set Y Position
        /// </summary>
        public int Y
        {
            get { return _y; }
            set { _y = value; }
        }


        public void Draw()
        {

            Console.SetCursorPosition(X, Y);
            Console.BackgroundColor = BackgroundColor;
            Console.ForegroundColor = Foreground;

            //Retrive Max Width for Items
            int max_width = 0;
            for (int i = 0; i < Item.Count; i++)
            {
                if (Item[i].Length > max_width) { max_width = Item[i].Length; } 
            }


            //--- TRANCE 1 ----//            
            Console.Write(_topleft);

            for (int i = 0; i < max_width; i++)
            {
                Console.Write(_hline);
            }

            Console.Write(_topright);

            //----------

            //Trance 2

            for (int i = 0; i < Item.Count; i++)
            {
                if (Selected_Index == i) { Console.BackgroundColor = ConsoleColor.DarkBlue; }
                if (Selected_Index != i) { Console.BackgroundColor = BackgroundColor; }

                Console.SetCursorPosition(X, Y + 1 + i);

                //1:
                Console.Write(_vline);
                for (int j = 0; j < max_width; j++)
                {

                    Console.Write(" ");
                }
                Console.Write(_vline);

                //2:
                
                Console.SetCursorPosition(X+1, Y + 1 + i);              
                Console.Write(Item[i]);

                //Fill Empy Space

                int emp = Item[i].Length - max_width;

                for (int k = 0; k < emp; k++)
                {
                    Console.Write(" ");
                }


                /*
                Console.SetCursorPosition(X, Y + 1 + i);
                Console.Write(_vline);
                Console.Write(Item[i]);

                Console.SetCursorPosition(X + max_width + 1, Y + 1 + i);
                Console.Write(_vline);*/
            }
            


            //--- Trance 3-----//

            Console.SetCursorPosition(X, Y + 1 + Item.Count);
            Console.Write(_bleft);

            for (int i = 0; i < max_width; i++)
            {
                Console.Write(_hline);
            }

            Console.Write(_bright);

        }

    }
}
